<template>
  <div class="mod-config">
    <basic-container>
      <div class="avue-crud">
        <el-radio-group
          v-model="tabType"
          @change="getDataList"
          style="margin-bottom: 10px"
        >
          <el-radio-button
            label="1"
            v-if="
              formName != 'jgtb' && formName != 'jghzsh' && formName != 'tbsh'
            "
            >通用指标</el-radio-button
          >
          <el-radio-button
            label="2"
            v-if="
              formName != 'jgtb' && formName != 'jghzsh' && formName != 'tbsh'
            "
            >个性指标</el-radio-button
          >
          <el-radio-button
            label="3"
            v-if="
              formName == 'jgtb' || formName == 'jghzsh' || formName == 'tbsh' || formName == 'jghz'
            "
            >奖惩</el-radio-button
          >
        </el-radio-group>
        <div class="pull-right">
          <el-button type="primary" @click="addZhibiaoku()"
            >新增指标库</el-button
          >
        </div>
        <el-table
          :data="dataList"
          stripe
          tooltip-effect="light"
          ref="tableRef"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column prop="normLx" label="指标类型" min-width="150" header-align="center"
            align="center">
          </el-table-column>
          <el-table-column
            prop="normName"
            label="指标名称"
            min-width="150"
            header-align="center"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column prop="normDw" label="指标单位" min-width="150" header-align="center"
            align="center">
          </el-table-column>
          <el-table-column prop="normGsname" label="计算公式" min-width="150" header-align="center"
            align="center">
          </el-table-column>
          <el-table-column
            prop="normSm"
            show-overflow-tooltip
            label="指标说明"
            min-width="200"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="normPx"
            label="加扣范围"
            min-width="100"
            v-if="tabType == 3"
          >
            <template slot-scope="scope">
              <span> {{ scope.row.normjka }}-{{ scope.row.normjkb }}</span>
              <span></span>
            </template>
          </el-table-column>
          <el-table-column prop="normPx" label="指标序号" min-width="80" header-align="center"
            align="center">
          </el-table-column>
          <el-table-column label="操作" align="center" min-width="240">
            <template slot-scope="scope">
              <el-button
                v-if="tabType == 1"
                type="primary"
                plain
                @click="pushHandle(scope.row.normId)"
                >推送</el-button
              >
              <el-button
                type="primary"
                plain
                @click="copyHandle(scope.row.normId)"
                >复制</el-button
              >
              <el-button type="primary" @click="editZhiBiao(scope.row)" plain
                >编辑</el-button
              >
              <el-button
                type="primary"
                @click="delZhiBiao(scope.row, scope.$index)"
                plain
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <table-form
          v-if="addOrUpdateVisible"
          ref="addOrUpdate"
          @refreshDataList="getDataList"
        ></table-form>
      </div>
    </basic-container>
  </div>
</template>
<script>
import { fetchList } from "@/api/hetongrenwu/zhibiaoTable";
import { getNormByTypeAndDeptId } from "@/api/hetongrenwu/hetongyeji/hetongzb";
import { delObj, copyHandle, pushHandle } from "@/api/admin/sysnorm";
import { mapGetters } from "vuex";
import TableForm from "@/views/admin/sysnorm/sysnorm-form";
export default {
  data() {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      tabType: "2",
      text: "",
      addOrUpdateVisible: true,
      enter: -1,
      multipleSelection: [],
      formName: "",
    };
  },
  props: {
    hetongId: Number,
  },
  computed: {
    ...mapGetters({
      userInfo: "userInfo",
    }),
  },
  components: {
    TableForm,
  },
  methods: {
    copyHandle(id) {
      this.$confirm("是否复制这条指标？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return copyHandle(id);
        })
        .then((data) => {
          this.$message.success("复制成功");
          this.getDataList();
        });
    },
    pushHandle(id) {
      this.$confirm("是否推送这条指标？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return pushHandle(id);
        })
        .then((data) => {
          this.$message.success("推送成功");
          this.getDataList();
        });
    },
    //从合同创建进入时
    hetongEnter(enter, formName) {
      this.enter = enter;
      this.formName = formName;
      this.getDataList();
    },
    handleSelectionChange(val) {
      if (this.enter == 0) {
        this.$emit("func", val);
      }
    },
    init(fname, tabtypes) {
      this.formName = fname;
      this.tabType = tabtypes;
    },
    // 获取数据列表
    getDataList() {
      this.addOrUpdateVisible = false;
      if (this.tabType == "1") {
        getNormByTypeAndDeptId({ normType: "htrw_zb", normFl: "ty" }).then(
          (response) => {
            this.dataList = response.data.data.data;
          }
        );
      } else if (this.tabType == "2") {
        getNormByTypeAndDeptId({
          normType: "htrw_zb",
          normFl: "gx",
          deptId: this.userInfo.deptId,
        }).then((response) => {
          this.dataList = response.data.data.data;
        });
      } else {
        getNormByTypeAndDeptId({ normType: "htrw_zb", normFl: "jc" }).then(
          (response) => {
            this.dataList = response.data.data.data;
          }
        );
      }
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    saveZhibiao(e) {
      const rows = this.$refs.tableRef.selection;
      if (rows.length < 1) {
        this.$message.closeAll();
        this.$message({
          type: "info",
          message: "请选择指标添加",
        });
      } else {
        this.duibiArr(e, rows);
        // debugger;
        if (this.text) {
          this.$confirm("指标名称：" + this.text + "重复", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(function () {
              this.$emit("func", rows);
            })
            .catch((data) => {});
        } else {
          this.$emit("func", rows);
        }
      }
    },
    // 对比重复
    duibiArr(oldArr, selectArr) {
      //console.log(selectArr)
      selectArr.forEach((item) => {
        var sameObj = oldArr.find(
          (oldItem) => oldItem.zhibiaoName == item.normName
        );
        //console.log(sameObj)
        if (sameObj) {
          this.text = this.text + sameObj.zhibiaoName + "、";
        } else {
          this.text = "";
        }
      });
    },
    addZhibiaoku(id) {
      this.addOrUpdateVisible = true;
      console.log(this.tabType);
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, "htrw_zb", "htrw_zb", this.tabType);
      });
    },
    //编辑指标库
    editZhiBiao(row) {
      //console.log(row)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(
          row.normId,
          "htrw_zb",
          "htrw_zb",
          this.tabType
        );
      });
    },
    //删除指标库
    delZhiBiao(row, index) {
      this.$confirm("是否确认删除" + row.normName + "指标", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(row.normId);
        })
        .then((data) => {
          this.$message.closeAll();
          this.$message.success("删除成功");
          this.getDataList();
        });
    },
  },
};
</script>
